using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Voodoo.Sauce.Internal.Analytics;

namespace Voodoo.Sauce.Debugger
{
	public class EventConsoleFiltersScreen : Screen
	{
		private const string DEFAULT_WRAPPER_NAME_FILTER = "VoodooAnalytics";

		[Header("EventFilters")]
		[SerializeField]
		private Transform filtersContainer;

		private readonly Queue<DebugToggleButton> _eventFilterItemsPool = new Queue<DebugToggleButton>();

		private readonly List<string> _excludedEventFilters = new List<string>();

		internal EventConsoleListScreen EventConsoleListScreen { private get; set; }

		private void OnEnable()
		{
			ClearFiltersBody();
			FillFiltersBody();
		}

		public override void OnScreenHide()
		{
			base.OnScreenHide();
			EventConsoleListScreen.RefreshEventLogToScreen();
		}

		private IEnumerable<KeyValuePair<string, bool>> GetFiltersWithStates()
		{
			SortedDictionary<string, bool> filtersStates = new SortedDictionary<string, bool>();
			foreach (DebugAnalyticsLog item in AnalyticsEventLogger.GetInstance().GetLocalAnalyticsLog("VoodooAnalytics"))
			{
				filtersStates[item.EventName] = true;
			}
			if (_excludedEventFilters != null)
			{
				foreach (string item2 in _excludedEventFilters.Where((string filter) => filtersStates.ContainsKey(filter)))
				{
					filtersStates[item2] = false;
				}
			}
			return filtersStates;
		}

		private void ClearFiltersBody()
		{
			foreach (Transform item in filtersContainer)
			{
				item.gameObject.SetActive(value: false);
				_eventFilterItemsPool.Enqueue(item.GetComponent<DebugToggleButton>());
			}
		}

		private void FillFiltersBody()
		{
			foreach (KeyValuePair<string, bool> filterState in GetFiltersWithStates())
			{
				DebugToggleButton debugToggleButton;
				if (_eventFilterItemsPool.Count == 0)
				{
					debugToggleButton = Toggle("", value: false, null);
					debugToggleButton.transform.SetParent(filtersContainer);
				}
				else
				{
					debugToggleButton = _eventFilterItemsPool.Dequeue();
					debugToggleButton.gameObject.SetActive(value: true);
					debugToggleButton.SetCallback(null);
				}
				debugToggleButton.Initialize(filterState.Key, filterState.Value, delegate(bool newState)
				{
					UpdateSavedFilterList(filterState.Key, newState);
				});
			}
		}

		private void UpdateSavedFilterList(string filter, bool isChecked)
		{
			if (isChecked && _excludedEventFilters.Contains(filter))
			{
				_excludedEventFilters.Remove(filter);
			}
			else if (!isChecked && !_excludedEventFilters.Contains(filter))
			{
				_excludedEventFilters.Add(filter);
			}
		}

		public bool IsExcluded(DebugAnalyticsLog log)
		{
			if (_excludedEventFilters != null)
			{
				return _excludedEventFilters.Any((string filter) => filter == log.EventName);
			}
			return false;
		}

		public IEnumerable<DebugAnalyticsLog> FilterEvents(IEnumerable<DebugAnalyticsLog> analyticsLogs)
		{
			if (_excludedEventFilters != null && _excludedEventFilters.Any())
			{
				analyticsLogs = analyticsLogs.Where((DebugAnalyticsLog analyticLog) => _excludedEventFilters.All((string filter) => filter != analyticLog.EventName));
			}
			return analyticsLogs;
		}
	}
}
